package com.xydtech.ecard.user.controller;

import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.service.IEcardGridManageService;
import com.xydtech.ecard.config.service.IEcardParamConfigService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "电子名片-用户相关")
@RequestMapping("/ecardSaasApi/user")
public class EcardApiUserController extends EcardApiBaseController {

    @Autowired
    private IEcardApiUserService userService;
    @Autowired
    private IEcardParamConfigService paramConfigService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardGridManageService ecardGridManageService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-用户相关-获取用户信息
     *
     * @return
     */
    @PassToken
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "电子名片-用户相关-获取用户信息", notes = "电子名片-用户相关-获取用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "custmanagerId", value = "客户经理ID", dataType = "String", dataTypeClass = String.class, paramType = "query", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiUserInfo.class)})
    @ResponseBody
    @RsaRequest(param = false, result = true)
    public ResponseModel getUserInfo() {
        String salt = getSalt();//盐值
        String currentUserId = getCurrentUserId();
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.objIsNullOrEmpty(currentUserId)) {
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            String appId = wechatApplet.getAppid();
            if (StringUtils.isEmpty(appId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
            }
            /**
             * 获取员工用户信息
             */
            EcardApiUserInfo ecardUserInfo = userService.getUserInfo(currentUserId, salt);
            //组装用户信息
            map.put("userInfo", ecardUserInfo);
        }
        return ResponseModel.okToJSON(map);
    }

    /**
     * 电子名片-用户相关-根据机构ID获取客户经理
     *
     * @return
     */
    @GetMapping("/getUserListByDeptId")
    @ApiOperation(value = "电子名片-用户相关-根据机构ID获取客户经理", notes = "电子名片-用户相关-根据机构ID获取客户经理")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "部门ID", dataType = "String", dataTypeClass = String.class, paramType = "query", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiUserInfo.class)})
    @ResponseBody
    @RsaRequest(param = false)
    public ResponseModel getUserListByDeptId(String deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
        }

        List<EcardApiUserInfo> userInfoList = userService.getUserListByDeptId(deptId);

        return ResponseModel.okToJSONArray(userInfoList);
    }

    /**
     * 电子名片-员工相关-修改个人信息
     * yjs 2022年11月25日13:32:23
     *
     * @return
     */
    @PostMapping(value = "/updatePersonalInfo", consumes = "application/json")
    @ApiOperation(value = "电子名片-用户相关-修改个人信息", notes = "电子名片-用户相关-修改个人信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "ecardUserInfo", value = "用户信息", dataType = "EcardApiUserInfo", dataTypeClass = EcardApiEcardRegistInfo.class, paramType = "body", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel updatePersonalInfo(@RequestBody @Validated EcardApiUserInfo ecardUserInfo) {
        String salt = getSalt();//盐值
        String registId = getCurrentUserId();
        //获取员工用户信息
        EcardApiUserInfo userInfo = userService.getUserInfo(registId, salt);
        //从缓存获取注册ID
        ecardUserInfo.setRegistId(registId);
        ecardUserInfo.setUserId(userInfo.getUserId());
        try {
            userService.updatePersonalInfo(ecardUserInfo, salt);
        } catch (Exception e) {
            e.getMessage();
            return ResponseModel.error("更新失败，请联系管理员处理");
        }
        return ResponseModel.ok();
    }

}
